<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/12/15 17:38
 * Desc:
 */
class Model_Spot_Ticket_People_Rule extends ORM
{
    protected $_table_name = 'spot_ticket_people_rule';

    public static function del_by_group_id($people_group_id)
    {
        DB::delete('spot_ticket_people_rule')->where('people_group_id', '=', $people_group_id)->execute();
    }

    // 门票人群规则
    public static function get_people_rules($spot_id, $ticket_id)
    {
        // 景区全部规则
        $people_groups = Model_Spot_People_Config::get_spot_edit_config($spot_id);
        $valid_groups  = array(); // 有效的人群数据
        foreach ($people_groups as $people_group) {
            if ($people_group['id']) {
                $valid_groups[$people_group['group_id']] = array(
                    'people_group_id' => $people_group['group_id'],
                    'name' => $people_group['name'],
                    'des' => $people_group['des'],
                    'rule' => false,
                );
            }
        }

        if (!empty($valid_groups)) {
            // 门票规则
            $people_rules = DB::select()
                ->from('spot_ticket_people_rule')
                ->where('spot_id', '=', $spot_id)
                ->and_where('ticket_id', '=', $ticket_id)
                ->execute()
                ->as_array();
            foreach ($people_rules as $people_rule) {
                if (isset($valid_groups[$people_rule['people_group_id']])) {
                    $valid_groups[$people_rule['people_group_id']]['rule'] = $people_rule;
                }
            }
        }

        return $valid_groups;
    }

    /**
     * 获取门票配置的人群相关规则
     * @param $ticket_id
     * @param int $show_age_type 是否显示年龄计算方式，默认 1 显示；
     * @return array|mixed
     */
    public static function get_ticket_people_rule($ticket_id, $show_age_type = 1)
    {
        // 门票规则
        $people_rule = DB::select('a.*', 'b.name', 'c.type', 'c.start_age', 'c.start_age_type', 'c.end_age', 'c.end_age_type', 'c.age_type')
            ->from(array('spot_ticket_people_rule', 'a'))
            ->join(array('spot_people_group', 'b'), 'inner')
            ->on('b.id', '=', 'a.people_group_id')
            ->join(array('spot_people_config', 'c'), 'inner')
            ->on('c.people_group_id', '=', 'a.people_group_id')
            ->on('c.spot_id', '=', 'a.spot_id')
            ->where('ticket_id', '=', $ticket_id)
            ->execute()
            ->current();

        $data = array();
        if (!empty($people_rule)) {
            $data['des'] = Model_Spot_People_Config::make_config_des($people_rule, $show_age_type);
            $ticket_des = self::make_des($people_rule);
            $data = array_merge($data, $ticket_des);
            $data['is_free'] = $people_rule['is_free'];
            $data['buy_type'] = $people_rule['buy_type'];
            $data['buy_min_num'] = $people_rule['buy_min_num'];
            $data['buy_max_num'] = $people_rule['buy_max_num'];
            $data['unit_num'] = $people_rule['unit_num'];
            $data['name'] = $people_rule['name'];
            $data['type'] = $people_rule['type'];
            $data['start_age'] = $people_rule['start_age'];
            $data['start_age_type'] = $people_rule['start_age_type'];
            $data['end_age'] = $people_rule['end_age'];
            $data['end_age_type'] = $people_rule['end_age_type'];
            $data['age_type'] = $people_rule['age_type'];
        }

        return !empty($data) ? $data : '';
    }

    // 生成规则描述
    public static function make_des($ticket_people_rule)
    {
        $data = array(
            'unit_num_des' => $ticket_people_rule['unit_num'] . '人/份',
            'is_free_des' => $ticket_people_rule['is_free'] == 1 ? '免费' : '自费',
            'buy_type_des' => $ticket_people_rule['buy_type'] == 1 ? '可单独购买' : '不可单独购买',
        );

        $buy_num = array();
        if ($ticket_people_rule['buy_min_num']) {
            $buy_num[] = '最少购买'.$ticket_people_rule['buy_min_num'].'份';
        }
        if ($ticket_people_rule['buy_max_num']) {
            $buy_num[] = '最多购买'.$ticket_people_rule['buy_min_num'].'份';
        }
        $data['buy_num_des'] = !empty($buy_num) ? implode(',', $buy_num) : '';
        return $data;
    }

    /**
     * 保存退款规则
     * @param $spot_id
     * @param $ticket_id
     * @param $params
     * @return void
     * @throws Kohana_Exception
     */
    public static function save_rule($spot_id, $ticket_id, $params)
    {
        $rule_ids = array();

        $people_group_id = Arr::get($params, 'people_group_id', '');
        $unit_num        = Arr::get($params, 'unit_num', '');
        $is_free         = Arr::get($params, 'is_free', '');
        $buy_type        = Arr::get($params, 'buy_type', '');
        $buy_min_num     = Arr::get($params, 'buy_min_num', '');
        $buy_max_num     = Arr::get($params, 'buy_max_num', '');

        if (!empty($people_group_id) && $unit_num && $is_free && $buy_type) {
            $orm = ORM::factory('spot_ticket_people_rule')
                ->where('spot_id', '=', $spot_id)
                ->and_where('ticket_id', '=', $ticket_id)
                ->and_where('people_group_id', '=', $people_group_id)
                ->find();
            if (empty($orm->id)) {
                $orm                  = ORM::factory('spot_ticket_people_rule');
                $orm->addtime         = time();
                $orm->spot_id         = $spot_id;
                $orm->ticket_id       = $ticket_id;
                $orm->people_group_id = $people_group_id;
            }
            $orm->modtime     = time();
            $orm->unit_num    = $unit_num[$people_group_id] ?? 1;
            $orm->is_free     = $is_free[$people_group_id] ?? 0;
            $orm->buy_type    = $buy_type[$people_group_id] ?? 2;
            $orm->buy_min_num = isset($buy_min_num[$people_group_id]) && $buy_min_num[$people_group_id] ? $buy_min_num[$people_group_id] : '';
            $orm->buy_max_num = isset($buy_max_num[$people_group_id]) && $buy_max_num[$people_group_id] ? $buy_max_num[$people_group_id] : '';
            $orm->save();
            if ($orm->saved()) {
                $rule_ids[] = $orm->id;
            }
        }

        // 删除不存在的值
        $del_builder = DB::delete('spot_ticket_people_rule')
            ->where('spot_id', '=', $spot_id)
            ->and_where('ticket_id', '=', $ticket_id);
        if (!empty($rule_ids)) {
            $del_builder->and_where('id', 'not in', $rule_ids);
        }
        $del_builder->execute();
    }
}